﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using com.hyrentals.webcommon.filter;
using com.hyrentals.webcommon.api;
using System.Web.Mvc;
using com.hyrentals.webcommon.extensions;
using com.hyrentals.common.image;
using System.IO;

namespace com.hyrentals.webportal.Controllers
{
    [OnImageError(ComponentType.Image)]
    public class ImageController : BaseController
    {
        [HttpPost]
        public ActionResult Upload(HttpPostedFileBase fileData, byte source)
        {
            ImageSourceEnum src = (ImageSourceEnum)source;
            if (!User.Identity.IsAuthenticated)
            {
                return this.JsonFail(CommonErrorId.Unauhtorized, "Unauhtorized.");
            }

            string newImageId = ImageManagerService.GenerateImageId(src);
            string imageExtension = Path.GetExtension(fileData.FileName);
            ImageManagerService.InsertImage(newImageId, fileData.InputStream, imageExtension);

            return Json(new JsonResultObj
            {
                Result = ResultStatusCode.Success,
                Content = new { id = newImageId }
            }, JsonRequestBehavior.AllowGet);

        }
    }
}